package excel

// 保存激活 WorkSheet
func (x *xlsx) setAcitveSheet(active_index int, active_name string) {
	x.active_index = active_index
	x.active_name = active_name
	x.index_name[active_index] = active_name
	x.name_index[active_name] = active_index
}

// 撤销激活 WorkSheet
func (x *xlsx) unsetAcitveSheet(active_index int, active_name string) {
	//x.active_index = active_index //激活状态不允许
	//x.active_name = active_name   //激活状态不允许
	delete(x.index_name, active_index)
	delete(x.name_index, active_name)
}

func (x *xlsx) GetSheetIndex(sheetname string) int {
	return x.name_index[sheetname]
}

func (x *xlsx) GetSheetName(index int) string {
	return x.index_name[index]
}

// 设置 activesheet
// index 数字或"Sheet1"
func (x *xlsx) SetActiveSheet(index any) error {
	active_name, active_index, e := x.getSheetAndIndex(index)
	if e != nil {
		return e
	}

	x.setAcitveSheet(active_index, active_name)
	x.f.SetActiveSheet(active_index)

	return nil
}

// 获取 activesheet
func (x *xlsx) GetActiveSheet() (sheetname string, index int) {
	return x.active_name, x.active_index
}

// 增加sheet
func (x *xlsx) NewSheet(sheetname string) int {
	active_index := x.f.NewSheet(sheetname)
	x.setAcitveSheet(active_index, sheetname)

	return x.f.NewSheet(sheetname)
}

// 复制sheet
func (x *xlsx) CopySheet(from_index any, to_index any) error {
	_, f_index, e := x.getSheetAndIndex(from_index)
	if e != nil {
		return e
	}

	_, t_index, e := x.getSheetAndIndex(to_index)
	if e != nil {
		return e
	}

	return x.f.CopySheet(f_index, t_index)
}

// 删除sheet
// index 数字或"Sheet1"
func (x *xlsx) DelSheet(index any) error {
	active_name, active_index, e := x.getSheetAndIndex(index)
	if e != nil {
		return e
	}
	x.unsetAcitveSheet(active_index, active_name)
	x.f.DeleteSheet(active_name)

	return nil
}

// 重命名
func (x *xlsx) SetSheetName(index any, newName string) error {
	active_name, _, e := x.getSheetAndIndex(index)
	if e != nil {
		return e
	}
	x.f.SetSheetName(active_name, newName)

	return nil
}

// 获取所有sheet
func (x *xlsx) GetAllSheet() map[int]string {
	return x.f.GetSheetMap()
}

// 是否可见
func (x *xlsx) SetSheetVisible(index any, visible bool) error {
	active_name, _, e := x.getSheetAndIndex(index)
	if e != nil {
		return e
	}
	if !visible && active_name == x.active_name {
		return Error_Cannot_Visible_Acitve_Sheet
	}

	x.f.SetSheetVisible(active_name, visible)

	return nil
}
func (x *xlsx) GetSheetVisible(index any) (bool, error) {
	active_name, _, e := x.getSheetAndIndex(index)
	if e != nil {
		return false, e
	}

	return x.f.GetSheetVisible(active_name), nil
}

// 背景图
func (x *xlsx) SetSheetBackground(index any, pictrue string) error {
	active_name, _, e := x.getSheetAndIndex(index)
	if e != nil {
		return e
	}

	return x.f.SetSheetBackground(active_name, pictrue)
}

// 给定 索引(index或name)返回 name,index
func (x *xlsx) getSheetAndIndex(index any) (string, int, error) {
	switch index.(type) {
	case int:
		active_index := index.(int)
		if active_name, ok := x.index_name[active_index]; ok {
			return active_name, active_index, nil
		}

		return "", 0, Error_Not_Found_Sheet

	case string:
		active_name := index.(string)
		if active_index, ok := x.name_index[active_name]; ok {
			return active_name, active_index, nil
		}

		return "", 0, Error_Not_Found_Sheet
	}

	return "", 0, Error_Not_Found_Sheet
}
